import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  deviceTypeData: [],
  alarmAllData: [],
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.alarm_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            let listDatas = response.data.result.list
            listDatas.data.map(item=>{
              item.company_arr.reverse()
            })
            commit("setLists", listDatas);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 150,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields.forEach(function (item, i) {
              item.dataIndex = item.key
              item.align = 'center';
              if(i < fields.length-2) {
                item.width = item.width || "12%"
              }
            })

            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  dataAdd({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.alarm_add, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  dataEdit({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.alarm_update, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        this.visible = false;
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // ID删除 > 参数为：url/id
  dataDel({commit}, id) {
    request.get(Server.action.alarm_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 所有设备类型信息
  deviceTypeAll({commit}) {
    request.get(Server.action.equipmentType_all)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setdeviceTypeAll", response.data.result);
      } else {
        showError( response.data.message )
      }
    })
  },
  // 所有报警联系人信息
  alarmAll({commit}, id) {
    request.post(Server.action.alarm_all, id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setAlarmAll", response.data.result);
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setdeviceTypeAll(state, data) {
    state.deviceTypeData = data;
  },
  setAlarmAll(state, data) {
    state.alarmAllData = data;
  },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
